current_edit = null
current_menu = null
current_cell = null
initial_value = null

function $(elt_id){
    return document.getElementById(elt_id)
}


function show_menu(ptype,num){
    if(current_cell!=null){
        return
    }
    path_type = ptype
    cell = $("cell_"+num)
    cell.style.color="red";
    current_cell = cell
    
    menu = $("menu_"+num)
    menu.firstChild.style.visibility="visible"
    current_menu=menu
}

function leave_cell(){
    if(current_cell==null){
        return
    }
    current_cell.style.color="#67B"
    current_cell = null
    
    current_menu.firstChild.style.visibility="hidden"
    current_menu = null
}

function drop_file(elt_id) {
    elt = $("name_"+elt_id);
    res = confirm("Delete "+elt.innerHTML+" ?")
    if(res){
        remove_path("file",elt_id)
    }
    return false;
}

function drop_dir(elt_id) {
    elt = $("name_"+elt_id);
    res = confirm("Delete "+elt.innerHTML+" ?")
    if(res){
        remove_path("dir",elt_id)
    }
    return false;
}

function start_edit(elt_id){
    if(current_edit!=null){
        end_edit()
    }
    elt = $("name_"+elt_id)
    elt.style.borderWidth=1;
    elt.style.borderColor="#000";
    elt.style.borderStyle="solid";
    initial_value = elt.innerHTML
    elt.contentEditable = true;
    elt.focus()
    elt.onblur = function(ev){
        end_edit()
    }
    current_edit = elt
}

function getRequestObject(){
    if (window.XMLHttpRequest)
      {// code for IE7+, Firefox, Chrome, Opera, Safari
      xmlhttp=new XMLHttpRequest();
      }
    else
      {// code for IE6, IE5
      xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
      }
    return xmlhttp;
}

function show_status(info){
    if(debug){
        zone = $("status")
        zone.value = info
    }
}

function show_msg(msg){
    document.getElementById("progress").innerHTML = msg
    window.setTimeout(function() {
        document.getElementById("progress").innerHTML = '&nbsp;'
         }, 3000); 
}

function remove_path(path_type,elt_id){
    elt = $("name_"+elt_id)
    path = elt.innerHTML
    show_status('remove '+elt.id)
    t = $("files")
    for(i=0;i<t.rows.length;i++){
        if(t.rows[i].id == 'row_'+elt_id){
            row_num = i
        }
    }
    
    xmlhttp2 = getRequestObject()
    xmlhttp2.onreadystatechange=function(){
        document.getElementById("progress").innerHTML = "sending, state "+xmlhttp.readyState
        if(xmlhttp2.readyState==4){
            window.clearTimeout(requestTimer);
            if(xmlhttp2.status==200){
                resp=xmlhttp2.responseText;
                show_status(resp)
                if(resp == "ok"){
                    show_msg("File "+elt.innerHTML+" deleted")
                    t = $("files")
                    t.deleteRow(row_num);
                } else {
                    resp=xmlhttp2.responseText;
                    alert(resp)                
                }
            } else {
                msg = '<b style="color:red;">Your changes could not been saved</b>'
                msg += ' - Error status '+xmlhttp.status
                document.getElementById("progress").innerHTML = msg
                resp=xmlhttp2.responseText; // issue_id if all went well
                alert(resp)
            }
        }
    }
    params = "folder="+folder
    params += "&path_type="+path_type
    params += "&src="+path
    requestTimeout = 3
    // if no reply after requestTimeOut seconds, abort request
    // found at http://ajaxpatterns.org/XMLHttpRequest_Call#Detecting_Errors
    requestTimer = window.setTimeout(function() {
           xmlhttp2.abort();
           show_status('Server did not within '+requestTimeout+' seconds - abort request')
         }, requestTimeout*1000); 
    
    try{
        xmlhttp2.open("POST","delete",true);
        xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp2.send(params);
    } catch(err) {
        document.getElementById("progress").innerHTML = "error "+err.description
        document.getElementById("progress").style.visibility = "visible" 
    }
}

function end_edit(){
    elt = current_edit
    elt.style.backgroundColor = "#FFF";
    elt.style.borderWidth=0;
    new_value = elt.innerHTML
    if(new_value==initial_value){
        return
    }
    show_status('remame '+initial_value+' to '+new_value)

    xmlhttp2 = getRequestObject()
    xmlhttp2.onreadystatechange=function(){
        document.getElementById("progress").innerHTML = "sending, state "+xmlhttp.readyState
        if(xmlhttp2.readyState==4){
            window.clearTimeout(requestTimer);
            //document.getElementById("progress").innerHTML = ""
            if(xmlhttp2.status==200){
                resp=xmlhttp2.responseText;
                show_status(resp)
                if(resp != "ok"){
                    msg = '<b style="color:red;">Your changes could not been saved</b>'
                    msg += ' - Error status '+xmlhttp.status
                    document.getElementById("progress").innerHTML = msg
                    // restore initial value
                    elt.innerHTML = initial_value
                } else {
                    // refresh page - order of files or dirs might have changed
                    location.href = "index?folder="+folder
                }
            } else {
                msg = '<b style="color:red;">Your changes could not been saved</b>'
                msg += ' - Error status '+xmlhttp.status
                document.getElementById("progress").innerHTML = msg
                // restore initial value
                elt.innerHTML = initial_value
            }
        }
    }
    params = "folder="+folder
    params += "&path_type="+path_type
    params += "&old_name="+initial_value
    params += "&new_name="+new_value
    requestTimeout = 3
    // if no reply after requestTimeOut seconds, abort request
    // found at http://ajaxpatterns.org/XMLHttpRequest_Call#Detecting_Errors
    requestTimer = window.setTimeout(function() {
           xmlhttp2.abort();
           show_status('Server did not within '+requestTimeout+' seconds - abort request')
         }, requestTimeout*1000); 
    
    try{
        xmlhttp2.open("POST","rename",true);
        xmlhttp2.setRequestHeader("Content-type","application/x-www-form-urlencoded");
        xmlhttp2.send(params);
    } catch(err) {
        document.getElementById("progress").innerHTML = "error "+err.description
        document.getElementById("progress").style.visibility = "visible" 
    }
    current_edit = null;
}
